<template>
	<view>
		<!-- <view :style="'height:'+statusbarH+'px'"></view> -->
		<view class="buttons_flex">
			<view class="button_bgcolor" @click="picFun(1)">
				{{ title }}
				<span>
					<u-icon name="arrow-down-fill" color="rgb(221, 221, 221)" size="10"></u-icon>
				</span>
			</view>
		</view>
		<u-picker :defaultIndex="defaultIndex" :show="show" v-if="show" :columns="columns" :keyName="name" @confirm="confirm" @cancel="show = false"></u-picker>
	</view>
</template>

<script>
	import request from '@/utils/request'
	export default {
		data() {
			return {
				defaultIndex: [0],
				show: false,
				statusbarH: '',
				columns: [[]],
			}
		},
		props: {
			value:{ // 当前的tab
				type:String,
				default: ''
			},
			title: {
				type: String,
				default: ''
			},
			url: {
				type: String,
				default: ''
			},
			dataType: {
				type: String,
				default: ''
			},
			name: {
				type: String,
				default: () => {
					return ''
				}
			},
			valueType: {
				type: String,
				default: () => {
					return 'dictValue'
				}
			}
			
		},
		methods:{
			picFun() {
				request.get(this.url, {
					pageNum: 1,
					pageSize: 1000,
				}).then((res) => {
					res[this.dataType].unshift({
						[this.name]: '全部',
						[this.valueType]: ''
					})
					if (this.title == '医院') {
						let arr = JSON.parse(uni.getStorageSync('userInfoRole')).hospitalList.map(item => item.hospitalId)
						res[this.dataType] = res[this.dataType].filter(item => arr.includes(item.id))
						res[this.dataType].unshift({
							[this.name]: '全部',
							[this.valueType]: ''
						})
					}
					this.columns[0] = res[this.dataType]
					
					this.columns[0].forEach((val, index) => {
						if(this.value == val[this.valueType]){
							this.defaultIndex[0] = index
						}
					})
					this.$nextTick(()=> {
						this.show = true
					})
				})
				
			},
			confirm(e) {
				uni.$emit('refreshfun', {
					type: this.title,
					data: e.value[0][this.valueType]
				})
				this.show = false
			},
		}
	}
</script>
<style  lang="scss" scoped>
	.button_bgcolor {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 160rpx;
		background-color: #fff;
		border-radius: 7rpx;
		padding: 20rpx 0;
		box-sizing: border-box;
		font-size: 28rpx;
	}
</style>